package com.haixiaoke.business;

import com.haixiaoke.common.annotation.Log;
import com.haixiaoke.common.core.domain.AjaxResult;
import com.haixiaoke.common.enums.BusinessType;
import com.haixiaoke.common.utils.poi.ExcelUtil;
import com.haixiaoke.domain.Model;
import com.haixiaoke.service.IModelService;
import com.haixiaoke.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 型号Controller
 *
 * @author zhiping
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/business/model")
public class ModelController extends BaseController {
    @Autowired
    private IModelService modelService;

    /**
     * 查询型号列表
     */
    @PreAuthorize("@ss.hasPermi('business:model:list')")
    @GetMapping("/list")
    public AjaxResult list(Model model) {
        return AjaxResult.success(modelService.selectModelList(model));
    }

    /**
     * 导出型号列表
     */
    @PreAuthorize("@ss.hasPermi('business:model:export')")
    @Log(title = "型号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Model model) {
        List<Model> list = modelService.selectModelList(model);
        ExcelUtil<Model> util = new ExcelUtil<Model>(Model.class);
        util.exportExcel(response, list, "型号数据");
    }

    /**
     * 获取型号详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:model:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(modelService.selectModelById(id));
    }

    /**
     * 新增型号
     */
    @PreAuthorize("@ss.hasPermi('business:model:add')")
    @Log(title = "型号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Model model) {
        return toAjax(modelService.insertModel(model));
    }

    /**
     * 修改型号
     */
    @PreAuthorize("@ss.hasPermi('business:model:edit')")
    @Log(title = "型号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Model model) {
        return toAjax(modelService.updateModel(model));
    }

    /**
     * 删除型号
     */
    @PreAuthorize("@ss.hasPermi('business:model:remove')")
    @Log(title = "型号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(modelService.deleteModelByIds(ids));
    }

}
